import { makeAutoObservable, } from "mobx";
import { showError, showLoading, showSuccess, } from "../components/Message";
import { ChangePasswordService } from "../services/ChangePasswordService";
import { ResearchDetailData } from "../services/ResearchDetailService";
import { SettingService } from "../services/SettingService";
import { userStore } from "./UserStore";
import GlobalConfig from "../config/GlobalConfig";
import { timestamp } from "../utils/globalFuncs";
import { UserInfo } from "../services/LoginService";

enum SettingsType {
  avatar = 'avatar',
  username = 'username',
  phone = 'phone',
  sex = 'sex',
  birthday = 'birthday',
  profile = 'profile',
  chatnotice = 'chatnotice',
  customerservicenotice = 'customerservicenotice',
  systemnotice = 'systemnotice'
}

class SettingStore {
  public detail?: ResearchDetailData;
  public loading: boolean = false;
  public type?: SettingsType;
  public passwordModalVisible: boolean = false;
  public editModalVisible: boolean = false;
  public editSingleModalVisible: boolean = false;
  public editType: string = 'username';


  constructor() {
    makeAutoObservable(this);
  }


  public async editSetting(data: any) {
    let _data: any = {};
    for (let key in data) {
      if (data[key]) {
        _data[key] = data[key];
      }
    }
    if (_data.birthday) {
      _data.birthday = _data.birthday.format("YYYY-MM-DD")
    }
    let originData = JSON.parse(JSON.stringify(_data));
    showLoading("编辑中...")
    try {
      await new SettingService(_data).send();
      if (userStore.userInfo) {
        let _userInfo: UserInfo = userStore.userInfo;
        for (let key in originData) {
          _userInfo[key] = originData[key];
        }
        userStore.updateUser(_userInfo);
      }
    } catch (e:any) {
      showError(e)
      return;
    }
    showSuccess('编辑成功！');
    this.editModalVisible = false;
    this.editSingleModalVisible = false;
  }


  public async changePssowrd(data: any) {
    showLoading("提交中...")
    try {
      await new ChangePasswordService(data).send();
    } catch (e:any) {
      showError(e)
      return;
    }
    this.passwordModalVisible = false;
    showSuccess('修改成功！');
  }


  public async OssUpload(file: any) {
    showLoading('上传中');
    var client = new (window as any).OSS.Wrapper({
      region: GlobalConfig.ossRegion, //阿里云获取
      accessKeyId: GlobalConfig.ossId,
      accessKeySecret: GlobalConfig.ossSecret,
      bucket: GlobalConfig.ossBucket  //要存储的目录名
    });
    var nameSplit = file.name.split(".");
    var fileName = timestamp() + '.' + nameSplit[nameSplit.length - 1];
    var storeAs = 'avatar/' + fileName;
    client.multipartUpload(storeAs, file).then(function (result: any) {
      var url = result.res.requestUrls[0];
      new SettingService({ avatar: url }).send().then(() => {
        showSuccess('上传成功！')
        if (userStore.userInfo) {
          var _userInfo: UserInfo = userStore.userInfo;
          _userInfo.avatar = url;
          userStore.updateUser(_userInfo);
        }

      }, () => {
        showError('上传失败！');
      });
    }).catch(function (err: any) {
      showError('上传失败！');
      console.log('oss upload error', err);
    });

  }

}

const settingStore = new SettingStore();

export { settingStore, SettingsType };